PostgreSQL 9.6 : Replication
2017/10/31 |
Configure PostgreSQL Replication settings. This configuration is Master-Slave settings.
|
|
[1] | |
[1] | Configure Master Host. |
[root@www ~]# postgresql-setup --initdb --unit rh-postgresql96-postgresql * Initializing database in '/var/opt/rh/rh-postgresql96/lib/pgsql/data' * Initialized, logs are in /var/lib/pgsql/initdb_rh-postgresql96-postgresql.log
[root@www ~]#
vi /var/opt/rh/rh-postgresql96/lib/pgsql/data/postgresql.conf # line 59: uncomment and change listen_addresses = ' * '
# line 178: uncomment and change wal_level = hot_standby
# line 183: uncomment and change # on ⇒ sync # remote_write ⇒ memory sync # local ⇒ slave is asynchronous # off ⇒ asynchronous synchronous_commit = local
# line 215: uncomment and change (enable archive_mode) archive_mode = on
# line 217: uncomment and change (command to get archives) archive_command = ' cp %p /var/opt/rh/rh-postgresql96/lib/pgsql/archive/%f '
# line 233: uncomment and change (slave servers + 1) max_wal_senders = 2
# line 235: uncomment and change wal_keep_segments = 10
# line 247: uncomment and change (any name you like) synchronous_standby_names = ' slave01 '
[root@www ~]#
vi /var/opt/rh/rh-postgresql96/lib/pgsql/data/pg_hba.conf # add to the end # host replication [replication user] [allowed IP addresses] password host replication replica 127.0.0.1/32 md5 host replication replica 10.0.0.30/32 md5 host replication replica 10.0.0.51/32 md5 systemctl start rh-postgresql96-postgresql [root@www ~]# systemctl enable rh-postgresql96-postgresql -bash-4.2$ createuser --replication -P replica Enter password for new role: Enter it again: |
[2] | If Firewalld is running, allow PostgreSQL service. |
[root@www ~]# firewall-cmd --add-service=postgresql --permanent success [root@www ~]# firewall-cmd --reload success |
[3] | Configure Slave Host. |
[root@node01 ~]#
su - postgres # get backup from Master Server -bash-4.2$ pg_basebackup -h www.srv.world -U replica -D /var/opt/rh/rh-postgresql96/lib/pgsql/data -P --xlog Password: # [replica] user's password 39211/39211 kB (100%), 1/1 tablespace
-bash-4.2$
vi /var/opt/rh/rh-postgresql96/lib/pgsql/data/postgresql.conf # line 256: uncomment and change hot_standby = on
-bash-4.2$
cp /opt/rh/rh-postgresql96/root/usr/share/pgsql/recovery.conf.sample /var/opt/rh/rh-postgresql96/lib/pgsql/data/recovery.conf -bash-4.2$ vi /var/opt/rh/rh-postgresql96/lib/pgsql/data/recovery.conf # line 44: uncomment and change (command to get archives) restore_command = ' scp 10.0.0.31:/var/opt/rh/rh-postgresql96/lib/pgsql/archive/%f %p '
# line 116: uncomment and change standby_mode = on
# line 123: uncomment and change (connection info to Master Server) primary_conninfo = ' host=10.0.0.31 port=5432 user=replica password=password application_name=slave01 '
exit logout [root@node01 ~]# systemctl start rh-postgresql96-postgresql [root@node01 ~]# systemctl enable rh-postgresql96-postgresql |
[4] | It's OK all if result of the command below is like follows. Make sure the setting works normally to create databases on Master Server. |
-bash-4.2$ psql -c "select application_name, state, sync_priority, sync_state from pg_stat_replication;" application_name | state | sync_priority | sync_state ------------------+-----------+---------------+------------ slave01 | streaming | 1 | sync (1 row) |